package interfata;

import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;

import javax.swing.*;

import visitor.CalculVisitor;

import arbori.ArboreFunctional;
import arbori.Nod;

//@SuppressWarnings("serial")
public class DesenFunctie extends JPanel 
{
	protected CalculVisitor fx = new CalculVisitor();
	/**
	 * PAD=dimensiunea unei unitati in pixeli.
	 */
	final int PAD = 10;
	/**
	 * scaleFactor=dimensiunea intre puncte
	 */
	final int scaleFactor = 10;
	protected ArboreFunctional a;

	public void setArbore(Nod n) 
	{
		this.a = new ArboreFunctional(n);
	}

	protected void paintComponent(Graphics g) 
	{
		int length = 200;
		/**
		 * data[][]=retine punctele si valorile functiei in punctele respective
		 */
		double data[][] = new double[2][length];
		/**
		 * val=punct
		 */
		double val = 0;

		/**
		 * Se populeaza data[][]: data[0][i]=x, data[1][i]=f(x);
		 */
		for (int i = 0; i < length - 1; i++) 
		{

			a.getRadacina().setX(val);
			a.acceptVisitor(fx);
			data[0][i] = val;
			data[1][i]=fx.getRezultat();
			val += 0.1;
		}
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setColor(Color.lightGray);
		g2.fillRect(0, 0, 1000, 600);
		
		int w = 750;
		int h = 400;

		/**
		 * Se reprezinta axa OY
		 */
		g2.setColor(Color.black);
		g2.draw(new Line2D.Double(PAD, PAD, PAD, h - PAD));

		/**
		 * Se reprezinta axa OX
		 */
		g2.draw(new Line2D.Double(PAD, h - PAD, w - PAD, h - PAD));

		/*
		  scale=ajuta la reprezentarea corecta a pctelor.
		 */
		double scale = (double) (h - 2 * PAD) / scaleFactor;

		/**
		 * Se reprezinta liniile dintre punctele
		 */
		g2.setPaint(Color.blue);
		for (int i = 0; i < length - 2; i++) 
		{
			double x1 = PAD + scale * data[0][i];
			double y1 = h - PAD - scale * data[1][i];
			double x2 = PAD + (data[0][i + 1]) * scale;
			double y2 = h - PAD - scale * data[1][i + 1];
			g2.draw(new Line2D.Double(x1, y1, x2, y2));
		}

		
		 //Se reprezinta punctele
		 
		/*for (int i = 0; i < length - 1; i++) 
		{
			double x = PAD + data[0][i] * scale;
			double y = h - PAD - scale * data[1][i];
			g2.fill(new Ellipse2D.Double(x - 1, y - 1, 2, 2));
		}*/
	}

}